<?php
use Base\AdminController;
use JetBrains\PhpStorm\NoReturn;

class RosterController extends AdminController
{
    public function init(): void
    {
        $this->routeWhiteLst = ['roster@addressbook'];
    }
    /**
     * 获取列表页数据
     * @return void
     */
    #[NoReturn] public function indexAction(): void
    {
        try {
            $sort = Request::input("sort");
            $orderBy = match ($sort)
            {
                "-id"=>'asc',
                default => 'desc',
            };
            if (Request::input("is_hid") !== null) {
                $this->where[] = ['is_hid','=',Request::input("is_hid")];
            }
            $sql = UserModel::query()->with(['organization','roles'])
                ->join("positions","positions.id","=","users.position_id")
                ->where($this->where)->when($this->whereIn,function ($query,$value){
                    foreach ($value as $item) {
                        $query->whereIn($item[0], $item[1]);
                    }
                });
            if (Request::input("time")) {
                if (is_array(Request::input("time"))) {
                    $sql->whereBetween("users.created_at",Request::input("time"));
                }else {
                    $sql->whereDate("users.created_at",Request::input("time"));
                }
            }
            if (Request::input("org_id")) {
                $sql->whereHas("organization",function ($query){
                   $query->where("id",Request::input("org_id"));
                });
            }
            $sql->selectRaw("ws_users.*,ws_positions.title as position_name");
            $data = $sql->orderBy("users.id",$orderBy)->paginate(Request::input("limit",10),['*'],NULL,Request::input("p",1));
            $result = [
                'items' => collect($data->items())->toArray(),
                'total'=>$data->total(),
                'per_page' => $data->perPage(),
                'page' => $data->currentPage()
            ];
            $this->apiSuccess($result,"获取成功",20000);
        }catch (Exception $e) {
            $this->apiError($e->getMessage());
        }
    }

    #[NoReturn] public function addressBookAction(): void
    {
        try {
            $sql = UserModel::query()->with(['organization','roles:id,title'])
                ->join("positions","positions.id","=","users.position_id");
            $sql->selectRaw("ws_users.id,ws_users.uuid,ws_users.username,ws_users.mobile,ws_users.nickname,ws_users.avatar,
            ws_positions.title as position_name");
            $data = $sql->get()->toArray();
            foreach ($data as $key=>$vo) {
                $uid = str_replace(['/','=','.'],"",yaf_encrypt($vo['id']));
                $data[$key]['uid'] = $uid;
                \Cache\Redis::getRedis(10)->set("user_token:".$uid,$vo);
            }
            $this->apiSuccess($data,"获取成功",20000);
        }catch (Exception $e) {
            $this->apiError($e->getMessage());
        }
    }
}
